package edu.ecnu.yt.spring.aop.annotation;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class Audience {
	// 定义一个切点
	@Pointcut("execution(** edu.ecnu.yt.spring.aop.annotation.Performance.performance(..))")
	public void performance() {};
	
	@Pointcut("execution(** edu.ecnu.yt.spring.aop.annotation.Performance.stop(String)) "
			+ "&& args(cause)")
	public void forceLeaved(String cause) {};
	
	@Around("forceLeaved(cause)")
	public void somethingHappens(ProceedingJoinPoint jp, String cause) {
		try {
			// 如果使用around则没有必要使用参数参数匹配切点表达式，因为jp可以自带args的查询。
			// System.out.println((String)jp.getArgs()[0]);
			System.out.println("Something has happened: " + cause);
			// 执行该连接点所具有的实际操作。
			jp.proceed();
			System.out.println("leaved");
		} catch (Throwable e) {
			System.out.println("cannot leave!!!");
		}
	}
	
	@Before("performance()")
	public void silenceCellPhones() {
		System.out.println("Silencing cell phones");
	}

	@Before("performance()")
	public void takeSeats() {
		System.out.println("Taking seats");
	}
	
	@AfterReturning("performance()")
	public void applause() {
		System.out.println("CLAP CLAP CLAP!!!");
	}
	
	@AfterThrowing("performance()")
	public void demandRefund() {
		System.out.println("Demanding a refund");
	}
}
